/*
*---------------------------------------------------------------
*                        Lvgl Font Tool                         
*                                                               
* 注:使用unicode编码                                              
* 注:本字体文件由Lvgl Font Tool V0.5测试版 生成                     
* 作者:阿里(qq:617622104)                                         
*---------------------------------------------------------------
*/




#ifdef LV_LVGL_H_INCLUDE_SIMPLE
#include "lvgl.h"
#else
#include "../lvgl/lvgl.h"
#endif



static const uint8_t glyph_bitmap[] = {
/* - */
0xff,0xff,  //@@@@


/* 0 */
0x07,0xef,0x80,  //.*#@%.
0x3c,0x11,0xc5,  //*#++#*
0xa5,0x00,0x4b,  //%*..*%
0xe0,0x00,0x0f,  //#....@
0xf0,0x00,0x0f,  //@....@
0xf0,0x00,0x0f,  //@....@
0xf0,0x00,0x0e,  //@....#
0xb4,0x00,0x4a,  //%*..*%
0x5c,0x11,0xc3,  //*#++#*
0x08,0xfe,0x70,  //.%@#*.


/* 1 */
0x00,0x3e,  //..*#
0x19,0xff,  //+%@@
0xfc,0x5f,  //@#*@
0x10,0x0f,  //+..@
0x00,0x0f,  //...@
0x00,0x0f,  //...@
0x00,0x0f,  //...@
0x00,0x0f,  //...@
0x00,0x0f,  //...@
0x00,0x0f,  //...@


/* 2 */
0x4c,0xff,0x90,  //*#@@%.
0xc4,0x01,0x9b,  //#*.+%%
0x00,0x00,0x0f,  //.....@
0x00,0x00,0x1e,  //....+#
0x00,0x00,0xc7,  //....#*
0x00,0x2c,0x90,  //..+#%.
0x06,0xe4,0x00,  //.*#*..
0x6c,0x00,0x00,  //*#....
0xd4,0x00,0x00,  //#*....
0xff,0xff,0xff,  //@@@@@@


/* 3 */
0x5d,0xfc,0x20,  //*#@#+
0xa1,0x06,0xd0,  //%+.*#
0x00,0x00,0xf0,  //....@
0x00,0x07,0x80,  //...*%
0x0f,0xfc,0x00,  //.@@#.
0x00,0x19,0xa0,  //..+%%
0x00,0x00,0xf0,  //....@
0x00,0x00,0xf0,  //....@
0xa1,0x09,0x90,  //%+.%%
0x7e,0xf9,0x00,  //*#@%.


/* 4 */
0x00,0x00,0x5f,0x00,  //....*@.
0x00,0x00,0xdf,0x00,  //....#@.
0x00,0x08,0xaf,0x00,  //...%%@.
0x00,0x3e,0x0f,0x00,  //..*#.@.
0x00,0xd4,0x0f,0x00,  //..#*.@.
0x0a,0x90,0x0f,0x00,  //.%%..@.
0x7c,0x00,0x0f,0x00,  //*#...@.
0xff,0xff,0xff,0xf0,  //@@@@@@@
0x00,0x00,0x0f,0x00,  //.....@.
0x00,0x00,0x0f,0x00,  //.....@.


/* 5 */
0x8f,0xff,0xff,  //%@@@@@
0xb3,0x00,0x00,  //%*....
0xb2,0x00,0x00,  //%+....
0xc0,0x00,0x00,  //#.....
0xff,0xfd,0x70,  //@@@#*.
0x00,0x04,0xc8,  //...*#%
0x00,0x00,0x0f,  //.....@
0x00,0x00,0x0e,  //.....#
0x20,0x02,0xc8,  //+..+#%
0xdf,0xfd,0x70,  //#@@#*.


/* 6 */
0x02,0xcf,0xf0,  //.+#@@.
0x0d,0x50,0x10,  //.#*.+.
0x77,0x00,0x00,  //**....
0xb1,0x00,0x00,  //%+....
0xf5,0xef,0xb0,  //@*#@%.
0xf9,0x00,0x9a,  //@%..%%
0xf0,0x00,0x0f,  //@....@
0xb1,0x00,0x0e,  //%+...#
0x5b,0x10,0x99,  //*%+.%%
0x07,0xef,0x90,  //.*#@%.


/* 7 */
0xff,0xff,0xff,0xf0,  //@@@@@@@
0x00,0x00,0x03,0x70,  //.....**
0x00,0x00,0x0b,0x00,  //.....%.
0x00,0x00,0x73,0x00,  //....**.
0x00,0x01,0xb0,0x00,  //...+%..
0x00,0x09,0x30,0x00,  //...%*..
0x00,0x1c,0x00,0x00,  //..+#...
0x00,0x85,0x00,0x00,  //..%*...
0x00,0xd0,0x00,0x00,  //..#....
0x01,0xc0,0x00,0x00,  //.+#....


/* 8 */
0x0a,0xff,0xa0,  //.%@@%.
0xb8,0x00,0x8b,  //%%..%%
0xf0,0x00,0x0f,  //@....@
0xb8,0x00,0x9b,  //%%..%%
0x0c,0xff,0xc0,  //.#@@#.
0x79,0x11,0x97,  //*%++%*
0xe0,0x00,0x0e,  //#....#
0xf0,0x00,0x0f,  //@....@
0x99,0x00,0x9a,  //%%..%%
0x09,0xff,0x90,  //.%@@%.


/* 9 */
0x09,0xff,0x80,  //.%@@%.
0x89,0x01,0xb5,  //%%.+%*
0xe0,0x00,0x1c,  //#...+#
0xf0,0x00,0x0f,  //@....@
0xa9,0x00,0x9f,  //%%..%@
0x0b,0xff,0x8f,  //.%@@%@
0x00,0x00,0x0c,  //.....#
0x00,0x00,0x58,  //....*%
0x09,0x03,0xd1,  //.%.*#+
0x07,0xfd,0x30,  //.*@#*.


/* : */
0xcc,  //##
0x66,  //**
0x00,  //..
0x00,  //..
0x00,  //..
0x66,  //**
0xcc,  //##


/* T */
0xff,0xff,0xff,0xf0,  //@@@@@@@
0x33,0x3f,0x33,0x30,  //***@***
0x00,0x0f,0x00,0x00,  //...@...
0x00,0x0f,0x00,0x00,  //...@...
0x00,0x0f,0x00,0x00,  //...@...
0x00,0x0f,0x00,0x00,  //...@...
0x00,0x0f,0x00,0x00,  //...@...
0x00,0x0f,0x00,0x00,  //...@...
0x00,0x0f,0x00,0x00,  //...@...
0x00,0x0f,0x00,0x00,  //...@...


};


static const lv_font_fmt_txt_glyph_dsc_t glyph_dsc[] = {
    {.bitmap_index = 0,	.adv_w = 6, .box_h = 1, .box_w = 4, .ofs_x = 1, .ofs_y = 7},/*(-)*/
    {.bitmap_index = 2,	.adv_w = 8, .box_h = 10, .box_w = 6, .ofs_x = 1, .ofs_y = 4},/*(0)*/
    {.bitmap_index = 32,	.adv_w = 8, .box_h = 10, .box_w = 4, .ofs_x = 1, .ofs_y = 4},/*(1)*/
    {.bitmap_index = 52,	.adv_w = 8, .box_h = 10, .box_w = 6, .ofs_x = 1, .ofs_y = 4},/*(2)*/
    {.bitmap_index = 82,	.adv_w = 8, .box_h = 10, .box_w = 6, .ofs_x = 1, .ofs_y = 4},/*(3)*/
    {.bitmap_index = 112,	.adv_w = 8, .box_h = 10, .box_w = 8, .ofs_x = 0, .ofs_y = 4},/*(4)*/
    {.bitmap_index = 152,	.adv_w = 8, .box_h = 10, .box_w = 6, .ofs_x = 1, .ofs_y = 4},/*(5)*/
    {.bitmap_index = 182,	.adv_w = 8, .box_h = 10, .box_w = 6, .ofs_x = 1, .ofs_y = 4},/*(6)*/
    {.bitmap_index = 212,	.adv_w = 8, .box_h = 10, .box_w = 8, .ofs_x = 0, .ofs_y = 4},/*(7)*/
    {.bitmap_index = 252,	.adv_w = 8, .box_h = 10, .box_w = 6, .ofs_x = 1, .ofs_y = 4},/*(8)*/
    {.bitmap_index = 282,	.adv_w = 8, .box_h = 10, .box_w = 6, .ofs_x = 1, .ofs_y = 4},/*(9)*/
    {.bitmap_index = 312,	.adv_w = 3, .box_h = 7, .box_w = 2, .ofs_x = 1, .ofs_y = 4},/*(:)*/
    {.bitmap_index = 319,	.adv_w = 7, .box_h = 10, .box_w = 8, .ofs_x = 0, .ofs_y = 4},/*(T)*/
};


static const uint16_t unicode_list_1[] = {
    0x002d,	/*(-)*/
    0x0030,	/*(0)*/
    0x0031,	/*(1)*/
    0x0032,	/*(2)*/
    0x0033,	/*(3)*/
    0x0034,	/*(4)*/
    0x0035,	/*(5)*/
    0x0036,	/*(6)*/
    0x0037,	/*(7)*/
    0x0038,	/*(8)*/
    0x0039,	/*(9)*/
    0x003a,	/*(:)*/
    0x0054,	/*(T)*/
    0x0000,    /*End indicator*/
};


static const lv_font_fmt_txt_cmap_t cmaps[] = {
    {
        .range_start = 0x002d,
        .range_length = 0x0054,
        .type = LV_FONT_FMT_TXT_CMAP_SPARSE_TINY,
        .glyph_id_start = 0,
        .unicode_list = unicode_list_1,
        .glyph_id_ofs_list = NULL,
        .list_length = 13,
    }
};


static lv_font_fmt_txt_glyph_cache_t glyph_cache;


static lv_font_fmt_txt_dsc_t font_dsc = {
    .glyph_bitmap = glyph_bitmap,
    .glyph_dsc = glyph_dsc,
    .cmaps = cmaps,
    .cmap_num = 1,
    .bpp = 4,

    .kern_scale = 0,
    .kern_dsc = NULL,
    .kern_classes = 0,

    .cache = &glyph_cache,
};


static int binsearch(const uint16_t *sortedSeq, int seqLength, uint16_t keyData) {
    int low = 0, mid, high = seqLength - 1;
    while (low <= high) {
        mid = (low + high)>>1;//右移1位等于是/2，奇数，无论奇偶，有个值就行
        if (keyData < sortedSeq[mid]) {
            high = mid - 1;//是mid-1，因为mid已经比较过了
        }
        else if (keyData > sortedSeq[mid]) {
            low = mid + 1;
        }
        else {
            return mid;
        }
    }
    return -1;
}


static const uint8_t * __user_font_get_bitmap(const lv_font_t * font, uint32_t unicode_letter) {
    lv_font_fmt_txt_dsc_t * fdsc = (lv_font_fmt_txt_dsc_t *) font->dsc;

    if( unicode_letter<fdsc->cmaps[0].range_start || unicode_letter>fdsc->cmaps[0].range_length ) return false;

    int i;
    if( unicode_letter==fdsc->cache->last_letter ){
        i = fdsc->cache->last_glyph_id;
    }
    else{
        i = binsearch(fdsc->cmaps[0].unicode_list, fdsc->cmaps[0].list_length, unicode_letter);
    }
    if( i != -1 ) {
        const lv_font_fmt_txt_glyph_dsc_t * gdsc = &fdsc->glyph_dsc[i];
        fdsc->cache->last_glyph_id = i;
        fdsc->cache->last_letter = unicode_letter;
        return &fdsc->glyph_bitmap[gdsc->bitmap_index];
    }
    return NULL;
}


static bool __user_font_get_glyph_dsc(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter, uint32_t unicode_letter_next) {
    lv_font_fmt_txt_dsc_t * fdsc = (lv_font_fmt_txt_dsc_t *) font->dsc;

    if( unicode_letter<fdsc->cmaps[0].range_start || unicode_letter>fdsc->cmaps[0].range_length ) return false;

    int i;
    if( unicode_letter==fdsc->cache->last_letter ){
        i = fdsc->cache->last_glyph_id;
    }
    else{
        i = binsearch(fdsc->cmaps[0].unicode_list, fdsc->cmaps[0].list_length, unicode_letter);
    }
    if( i != -1 ) {
        const lv_font_fmt_txt_glyph_dsc_t * gdsc = &fdsc->glyph_dsc[i];
        fdsc->cache->last_glyph_id = i;
        fdsc->cache->last_letter = unicode_letter;
        dsc_out->adv_w = gdsc->adv_w;
        dsc_out->box_h = gdsc->box_h;
        dsc_out->box_w = gdsc->box_w;
        dsc_out->ofs_x = gdsc->ofs_x;
        dsc_out->ofs_y = gdsc->ofs_y;
        dsc_out->bpp   = fdsc->bpp;
        return true;
    }
    return false;
}


//微软雅黑,Regular,10
//字模高度：19
//内部字体
//使用排序和二分查表
const lv_font_t font_time_10 = {
    .dsc = &font_dsc,
    .get_glyph_bitmap = __user_font_get_bitmap,
    .get_glyph_dsc = __user_font_get_glyph_dsc,
    .line_height = 19,
    .base_line = 0,
};

